package com.example.controller;

import com.example.domain.dto.result.ScheduleList;
import com.example.domain.dto.result.ScheduleResult;
import com.example.domain.entity.AutoScheduleSetting;
import com.example.domain.entity.Schedule;
import com.example.domain.query.ScheduleQuery;
import com.example.factory.ResultFactory;
import com.example.service.IScheduleService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.v3.oas.annotations.Operation;

import java.time.LocalDate;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/schedules")
@Tag(name = "排班管理器")
public class ScheduleController {
    @Autowired
    private IScheduleService scheduleService;

    @Autowired
    private ResultFactory resultFactory;

    @PostMapping("/page")
    @Operation(summary = "分页查询排班")
    public IPage<ScheduleResult> pageQuery(@RequestBody ScheduleQuery query,
                                           @RequestParam(defaultValue = "1") int pageNum,
                                           @RequestParam(defaultValue = "10") int pageSize) {
        return resultFactory.convertSchedulePage(scheduleService.pageQuery(query, pageNum, pageSize));
    }

    @PostMapping("/save")
    @Operation(summary = "新增排班")
    public boolean save(@RequestBody Schedule schedule) {
        return scheduleService.save(schedule);
    }

    @PutMapping
    @Operation(summary = "更新排班")
    public boolean update(@RequestBody Schedule schedule) {
        return scheduleService.updateById(schedule);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除排班")
    public boolean delete(@PathVariable Integer id) {
        return scheduleService.removeById(id);
    }

    // date 为查询的日期， range为查询的范围，day为当天， week为一周，month为一月
    // personId 为人员编号，若为null则查询所有人的排班
    @GetMapping("/getSchedules")
    @Operation(summary = "获取排班")
    public ScheduleList getSchedules(LocalDate date, String range, @RequestParam(required = false) Integer personId, Integer deptId) {
        List<Schedule> schedules = scheduleService.getSchedules(date, range, personId).stream()
                .filter(schedule -> schedule.getDeptId().equals(deptId) || deptId == null).toList();
        return  resultFactory.scheduleList(schedules, range, date);
    }

    @PostMapping("/autoSchedule")
    @Operation(summary = "自动排班")
    public String autoSchedule(@RequestBody AutoScheduleSetting setting) {
        return this.scheduleService.autoSchedule(setting);
    }

    @PostMapping("/applyLeave")
    @Operation(summary = "请假")
    public boolean applyLeave(@RequestBody Schedule schedule) {
        return scheduleService.applyLeave(schedule);
    }

    @PutMapping("/reviewLeave/{scheduleId}")
    @Operation(summary = "审批请假")
    public boolean reviewLeave(@PathVariable Integer scheduleId, @RequestParam Boolean approved) {
        return scheduleService.reviewLeave(scheduleId, approved);
    }
}
